package com.wenjh.jdmzt.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @author <a href="mailt:wenlin56@sina.com"> wjh </a>
 */
public class JsonUtil {

	private static Log4Android log = new Log4Android("JsonUtil");
	public static String object2json(Object obj) {
		StringBuilder json = new StringBuilder();
		if (obj == null) {
			json.append("\"\"");
		} else if (obj instanceof String || obj instanceof Integer || obj instanceof Float || obj instanceof Boolean || obj instanceof Short || obj instanceof Double || obj instanceof Long || obj instanceof BigDecimal || obj instanceof BigInteger || obj instanceof Byte) {
			json.append("\"").append(string2json(obj.toString())).append("\"");
		} else if (obj instanceof Object[]) {
			json.append(array2json((Object[]) obj));
		} else if (obj instanceof List) {
			json.append(list2json((List<?>) obj));
		} else if (obj instanceof Map) {
			json.append(map2json((Map<?, ?>) obj));
		} else if (obj instanceof Set) {
			json.append(set2json((Set<?>) obj));
		} else if (obj instanceof Date) {
//			 json.append("\"").append(string2json(""+DateUtil.formateTimestamp((Date)obj))).append("\"");
		} else {
			json.append(bean2json(obj));
		}
		return json.toString();
	}

	public static String bean2json(Object bean) {
		StringBuilder json = new StringBuilder();
		json.append("{");

		List<String> properties = getProperties(bean.getClass());
		Class clazz = bean.getClass();

		if (properties != null) {
			for (int i = 0; i < properties.size(); i++) {
				try {
					String property = properties.get(i);
					String name = object2json(property);

					String methodName = "get" + property.substring(0, 1).toUpperCase() + property.substring(1);
					String value = object2json(clazz.getMethod(methodName).invoke(bean));
					json.append(name);
					json.append(":");
					json.append(value);
					json.append(",");
				} catch (Exception e) {
				}
			}
			json.setCharAt(json.length() - 1, '}');
		} else {
			json.append("}");
		}
		return json.toString();
	}

	public static String list2json(List<?> list) {
		StringBuilder json = new StringBuilder();
		json.append("[");
		if (list != null && list.size() > 0) {
			for (Object obj : list) {
				json.append(object2json(obj));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, ']');
		} else {
			json.append("]");
		}
		return json.toString();
	}

	public static String array2json(Object[] array) {
		StringBuilder json = new StringBuilder();
		json.append("[");
		if (array != null && array.length > 0) {
			for (Object obj : array) {
				json.append(object2json(obj));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, ']');
		} else {
			json.append("]");
		}
		return json.toString();
	}

	public static String map2json(Map<?, ?> map) {
		StringBuilder json = new StringBuilder();
		json.append("{");
		if (map != null && map.size() > 0) {
			for (Object key : map.keySet()) {
				json.append(object2json(key));
				json.append(":");
				json.append(object2json(map.get(key)));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, '}');
		} else {
			json.append("}");
		}
		return json.toString();
	}

	public static String set2json(Set<?> set) {
		StringBuilder json = new StringBuilder();
		json.append("[");
		if (set != null && set.size() > 0) {
			for (Object obj : set) {
				json.append(object2json(obj));
				json.append(",");
			}
			json.setCharAt(json.length() - 1, ']');
		} else {
			json.append("]");
		}
		return json.toString();
	}

	public static String string2json(String s) {
		if (s == null)
			return "";
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			switch (ch) {
			case '"':
				sb.append("\\\"");
				break;
			case '\\':
				sb.append("\\\\");
				break;
			case '\b':
				sb.append("\\b");
				break;
			case '\f':
				sb.append("\\f");
				break;
			case '\n':
				sb.append("\\n");
				break;
			case '\r':
				sb.append("\\r");
				break;
			case '\t':
				sb.append("\\t");
				break;
			case '/':
				sb.append("\\/");
				break;
			default:
				if (ch >= '\u0000' && ch <= '\u001F') {
					String ss = Integer.toHexString(ch);
					sb.append("\\u");
					for (int k = 0; k < 4 - ss.length(); k++) {
						sb.append('0');
					}
					sb.append(ss.toUpperCase());
				} else {
					sb.append(ch);
				}
			}
		}
		return sb.toString();

	}

	public static List<String> getProperties(Class clazz) {
		List<String> allPropertis = new ArrayList<String>();

		Method[] methods = clazz.getMethods();

		for (Method method : methods) {
			String name = method.getName();
			if (name.startsWith("set")) {
				String property = name.substring(3, name.length());
				if (property != "") {
					property = property.substring(0, 1).toLowerCase() + property.substring(1);
					allPropertis.add(property);
				}
			}
		}

		return allPropertis;
	}
	
	
	public static void json2bean(String json, Object bean) {
		try {
			JSONObject jo = new JSONObject(json);
			Class clazz = bean.getClass();
			List<String> properties = getProperties(clazz);
			if (properties != null) {
				for (int i = 0; i < properties.size(); i++) {
					try {
						String property = properties.get(i);
						String methodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
						clazz.getMethod(methodName, Object.class).invoke(bean, jo.get(property));
					} catch (Exception e) {
					}
				}
			}
		} catch (JSONException e1) {
		}
	}
}